
package com.mobilerecognition.phonenumer.camera;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.mobilerecognition.phonenumer.R;
import com.mobilerecognition.phonenumer.general.CGlobal;
import com.mobilerecognition.phonenumer.utils.Tools;


/**
 * This view is overlaid on top of the camera preview. It adds the viewfinder rectangle and partial
 * transparency outside it, as well as the laser scanner animation and result points.
 */
public final class ViewfinderView extends View {

    private static final int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
    private static final long ANIMATION_DELAY = 200L;//100L;
    private static final int OPAQUE = 0xFF;

    private final Paint paint;
    private Bitmap resultBitmap;
    private final int frameColor;
    private final int laserColor;
    private int scannerAlpha;

    public int m_top = 0;
    public int m_height = 0;
    private float delta;

    private long stTime;

    Rect frameRec;


    private String scanText = "将条形码和手机号放入框中";


    // This constructor is used when the class is built from an XML resource.
    public ViewfinderView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Initialize these once for performance rather than calling them every time in onDraw().
        paint = new Paint();
        Resources resources = getResources();
        frameColor = Color.parseColor("#ffffff");
        laserColor = Color.parseColor("#ffffff");
        scannerAlpha = 0;
        paint.setAntiAlias(true);
        delta = 1.0F;
        delta = getResources().getDisplayMetrics().density / 1.5F;

        stTime = System.currentTimeMillis();
    }

    @Override
    public void onDraw(Canvas canvas) {
        Rect frame = CGlobal.g_rectPreview;

        if (frame == null) {
            return;
        }
        frameRec = frame;

        int width = canvas.getWidth();
        int height = canvas.getHeight();

        paint.setColor(0x00000000);//77777777);

        canvas.drawRect(0, 0, width, frame.top, paint);
        canvas.drawRect(0, frame.top, frame.left, frame.bottom + 1, paint);
        canvas.drawRect(frame.right + 1, frame.top, width, frame.bottom + 1, paint);
        canvas.drawRect(0, frame.bottom + 1, width, height, paint);

        if (resultBitmap != null) {
            paint.setAlpha(OPAQUE);
            canvas.drawBitmap(resultBitmap, frame.left, frame.top, paint);
        } else {

            paint.setColor(frameColor);
            canvas.drawRect(frame.left, frame.top, frame.right + 1, frame.top + 2, paint);
            canvas.drawRect(frame.left, frame.top + 2, frame.left + 2, frame.bottom - 1, paint);
            canvas.drawRect(frame.right - 1, frame.top, frame.right + 1, frame.bottom - 1, paint);
            canvas.drawRect(frame.left, frame.bottom - 1, frame.right + 1, frame.bottom + 1, paint);

            int conner;
            conner = (frame.bottom - frame.top) / 8;
            paint.setColor(laserColor);
            paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
            scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
            int middle = frame.height() / 2 + frame.top;
            canvas.drawRect(frame.left + 20, middle - 1, frame.right - 20, middle + 2, paint);
            postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom);
        }
        drawText(canvas,frame);
    }

    private Rect createRect(int left, int top, int right, int bottom) {
        int offset = (int) (2F * delta);
        Rect rect = new Rect();
        rect.left = Math.min(left, right) - offset;
        rect.right = Math.max(left, right) + offset;
        rect.top = Math.min(top, bottom) - offset;
        rect.bottom = Math.max(top, bottom) + offset;
        return rect;
    }

    private void drawText(Canvas canvas, Rect rect) {
        int margin = 40;
        paint.setColor(Color.parseColor("#ffffff"));
        paint.setTextSize(Tools.dip2px(getContext(),15));
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
        float offY = fontTotalHeight / 2 - fontMetrics.bottom;
        float newY = rect.bottom + margin + offY;
        float left = margin;
        canvas.drawText(scanText, left, newY, paint);
    }

    public void setScanText(String text){
        scanText = text;
        invalidate();
//        postInvalidateDelayed(800L, frameRec.left, frameRec.top, frameRec.right, frameRec.bottom);
    }

    public void drawViewfinder() {
        resultBitmap = null;
        invalidate();
    }


}
